package com.chinameyer.equipment_ebook.controller;

import com.chinameyer.equipment_ebook.bean.ManualType;
import com.chinameyer.equipment_ebook.common.result.ApiResponse;
import com.chinameyer.equipment_ebook.service.EventService;
import com.chinameyer.equipment_ebook.service.ManualTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author meyer@HongYe
 */
@RestController
@RequestMapping("/v1/manualType/")
@Validated
@CrossOrigin
public class ManualTypeController {

    @Autowired
    private ManualTypeService manualTypeService;
    @Autowired
    private EventService eventService;


    /**
     * 添加说明书类型
     * @param manualType 说明书类型
     * @return 添加结果响应
     */
    @PostMapping("add")
    public ApiResponse add(@RequestParam(name = "manualType") String manualType,
                           @RequestParam(name = "userId") Integer userId){
        boolean isExist = this.manualTypeService.checkIsExistManualType(manualType,userId);
        if(isExist){
            return new ApiResponse(400,"说明书类型已存在，请不要重复添加！",0);
        }
        try {
            this.manualTypeService.addManualType(manualType,userId);
            return new ApiResponse(200,"success",1);
        }catch (Exception e){
            return new ApiResponse(400,"说明书类型添加失败，请联系系统管理员！",0);
        }
    }


    /**
     * 删除说明书类型
     * @param manualTypeId 说明书类型ID
     * @return 删除结果响应
     */
    @PostMapping("delete")
    public ApiResponse delete(@RequestParam(name = "manualTypeId") Integer manualTypeId,
                              @RequestParam(name = "userId") Integer userId){
        boolean isContainsContext = this.eventService.getAllContext(manualTypeId,userId);
        if(isContainsContext){
            return new ApiResponse(400,"当前类型下存在说明书，故无法删除",0);
        }
        boolean isDeleteSuccess = this.manualTypeService.deleteManualType(manualTypeId,userId);
        if(!isDeleteSuccess){
            return new ApiResponse(400,"说明书类型删除失败，请联系系统管理！",0);
        }
        return new ApiResponse(200,"success",1);
    }


    /**
     * 更新说明书类型
     * @param manualTypeId 说明书类型ID
     * @param manualType   说明书类型名称
     * @return 更新结果响应
     */
    @PostMapping("update")
    public ApiResponse update(@RequestParam(name = "manualTypeId") Integer manualTypeId,
                              @RequestParam(name = "manualType") String manualType,
                              @RequestParam(name = "userId") Integer userId){
        boolean isExist = this.manualTypeService.checkIsExistManualTypeExpectItself(manualTypeId,manualType,userId);
        if(isExist){
            return new ApiResponse(400,"说明书类型已存在，请更换类型名称再更新！",0);
        }
        boolean isUpdateSuccess = this.manualTypeService.updateManualType(manualTypeId,manualType,userId);
        if(!isUpdateSuccess){
            return new ApiResponse(400,"说明书类型更新失败，请联系系统管理！",0);
        }
        return new ApiResponse(200,"success",1);
    }


    /**
     * 获取所有的说明书类型
     * @return 获取结果响应
     */
    @GetMapping("getList")
    public ApiResponse getList(@RequestParam(name = "userId") Integer userId){
        List<ManualType> manualTypeList = this.manualTypeService.getAllManualType(userId);
        return new ApiResponse(200,"success",manualTypeList);
    }

}
